package com.ys.controller.web.storage;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.storage.*;
import com.ys.entity.storage.TccLogisticsMethod;
import com.ys.entity.storage.TccLogisticsMethodItem;
import com.ys.service.storage.TccLogisticsMethodService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

@Controller
@RequestMapping("/web/storage/tccLogisticsMethod")
public class WebTccLogisticsMethodController {
	private static final Logger logger = LoggerFactory.getLogger(WebTccLogisticsMethodController.class);

	@Autowired
	private CommonApiService<TccLogisticsMethod> commonApiService;
	@Autowired
	private TccLogisticsMethodService tccLogisticsMethodService;

	private final Long MENU_ID = MenuId.TCC_LogisticsMethod;

	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response) {

		try {
			Map<String, Object> tccWarehouse = new TccWarehouse()
					.addField("A.Vck vck")
					.where("A.cck='CK00001'")
					.queryFirstMap();
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MENU_ID, new ArrayList<Map<String, Object>>());
			Map<String, Object> extraParam = new HashMap<String, Object>();
			extraParam.put("status", 1);
			extraParam.put("cck", "CK00001");
			extraParam.put("vck", tccWarehouse.get("vck"));
			commonApiService.toAdd(model, new TccLogisticsMethod(), MenuId.TCC_LogisticsMethod, extraParam, null);
			model.addAttribute("onProcess", 0);
			return "storage/tccLogisticsMethod/tccLogisticsMethodAdd";
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}

	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
		TccLogisticsMethod entity = new TccLogisticsMethod().setId(id).queryFirst();
		try {
			// 子列表配置信息
			Map<String, Object> tccLogisticsMethod = new TccLogisticsMethod()
					.addLeftJoin("TCC_Warehouse B ON B.Cck=A.Cck")
					.addLeftJoin("TB_Country C ON C.Code=A.DestinationCountryCode")
					.addLeftJoin("TCC_LogisticsMethodItemSon D ON D.MethodId=A.Id")
					.addField("B.Vck vck,C.Name destinationCountryName,D.AdditionalMaterialBilling additionalMaterialBilling")
					.setId(id)
					.queryFirstMap();
			Map<String, Object> extraParam = new HashMap<String, Object>();
			extraParam.put("cck", entity.getCck());
			extraParam.put("vck", tccLogisticsMethod.get("vck"));
			extraParam.put("premark", tccLogisticsMethod.get("premark"));
			commonApiService.toEdit(model, id, new TccLogisticsMethod(), MenuId.TCC_LogisticsMethod, extraParam, null);
			List<TccLogisticsMethodItem> tccLogisticsOne = getLogisticsOne(id);
			List<TccLogisticsMethodItemSon> tccLogisticsTwo = getLogisticsTwo(id);
			List<TccLogisticsMethodItem> tccLogisticsThree = getLogisticsThree(id);
			List<TccAdditionalBilling> tccLogisticsFour = getLogisticsFour(id);
			List<TccLogisticsCompanyApply> tccLogisticsFive = getLogisticsFive(id);
			List<TccAdditionalBillingTwo> tccLogisticsSix = getLogisticsSix(id);


			Long LogisticsMethodId = entity.getId();

			TccLogisticsMethodDetailEdit detailList = new TccLogisticsMethodDetailEdit(id, tccLogisticsOne, tccLogisticsTwo, tccLogisticsThree, tccLogisticsFour, tccLogisticsFive,tccLogisticsSix);
			model.addAttribute("detail", detailList);
			model.addAttribute("LogisticsMethodId", LogisticsMethodId);
			model.addAttribute("onProcess", 0);
			return "storage/tccLogisticsMethod/tccLogisticsMethodEdit";
		} catch (Exception e) {
			logger.error("物流方式维护表编辑页面出错：", e.getMessage(), e);
			return W.error(model, e.getMessage());
		}
	}

	//获取计重梯度计费标准数据
	public List<TccLogisticsMethodItem> getLogisticsOne(Long id) {
		List<TccLogisticsMethodItem> tccLogisticsOne = new TccLogisticsMethodItem()
				.setMethodId(id)
				.setLogisticsMethodType(0)
				.queryList();

		return tccLogisticsOne;
	}

	//获取材质附加计费标准数据
	public List<TccLogisticsMethodItemSon> getLogisticsTwo(Long id) {
		List<TccLogisticsMethodItemSon> tccLogisticsTwo = new TccLogisticsMethodItemSon()
				.setMethodId(id)
				.queryList();

		return tccLogisticsTwo;
	}

	//获取抛重附加计费标准数据
	public List<TccLogisticsMethodItem> getLogisticsThree(Long id) {
		List<TccLogisticsMethodItem> tccLogisticsThree = new TccLogisticsMethodItem()
				.setMethodId(id)
				.setLogisticsMethodType(1)
				.queryList();
		return tccLogisticsThree;
	}

	//获取目的地附加计费标准数据
	public List<TccAdditionalBilling> getLogisticsFour(Long id) {
		List<TccAdditionalBilling> tccLogisticsFour = new TccAdditionalBilling()
				.setMethodId(id)
				.queryList();
		return tccLogisticsFour;
	}

	//获取目的地附加计费标准数据
	public List<TccLogisticsCompanyApply> getLogisticsFive(Long id) {
		List<TccLogisticsCompanyApply> tccLogisticsFive = new TccLogisticsCompanyApply()
				.setMethodId(id)
				.queryList();
		return tccLogisticsFive;
	}
	//获取目的地附加计费标准数据
	public List<TccAdditionalBillingTwo> getLogisticsSix(Long id) {
		List<TccAdditionalBillingTwo> tccLogisticsSix = new TccAdditionalBillingTwo()
				.setMethodId(id)
				.queryList();
		return tccLogisticsSix;
	}


	@Data
	@AllArgsConstructor
	private static class TccLogisticsMethodDetailEdit {
		private Long id;
		private List<TccLogisticsMethodItem> tccLogisticsOne; // 计重梯度计费标准
		private List<TccLogisticsMethodItemSon> tccLogisticsTwo; // 材质附加计费标准
		private List<TccLogisticsMethodItem> tccLogisticsThree; // 抛重附加计费标准
		private List<TccAdditionalBilling> tccLogisticsFour; // 目的地附加计费标准
		private List<TccLogisticsCompanyApply> tccLogisticsFive; // 运营公司信息
		private List<TccAdditionalBillingTwo> tccLogisticsSix; // 可发送目的


	}

	/*
	 * 详情页
	 */
	@RequiresPermissions("tccLogisticsMethod:view")
	@GetMapping("/toDetail")
	public String toDetail(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
		TccLogisticsMethod entity = new TccLogisticsMethod().setId(id).queryFirst();
		try {
			// 子列表配置信息
			Map<String, Object> tccLogisticsMethod = new TccLogisticsMethod()
					.addLeftJoin("TCC_Warehouse B ON B.Cck=A.Cck")
					.addLeftJoin("TB_Country C ON C.Code=A.DestinationCountryCode")
					.addLeftJoin("TCC_LogisticsMethodItemSon D ON D.MethodId=A.Id")
					.addField("B.Vck vck,C.Name destinationCountryName,D.AdditionalMaterialBilling additionalMaterialBilling")
					.queryFirstMap();
			Map<String, Object> extraParam = new HashMap<String, Object>();
			extraParam.put("cck", entity.getCck());
			extraParam.put("vck", tccLogisticsMethod.get("vck"));
			commonApiService.toDetail(model, id, new TccLogisticsMethod(), MenuId.TCC_LogisticsMethod, extraParam, null);
			List<TccLogisticsMethodItem> tccLogisticsOne = getLogisticsOne(id);
			List<TccLogisticsMethodItemSon> tccLogisticsTwo = getLogisticsTwo(id);
			List<TccLogisticsMethodItem> tccLogisticsThree = getLogisticsThree(id);
			List<TccAdditionalBilling> tccLogisticsFour = getLogisticsFour(id);
			List<TccLogisticsCompanyApply> tccLogisticsFive = getLogisticsFive(id);
			List<TccAdditionalBillingTwo> tccLogisticsSix = getLogisticsSix(id);


			Long LogisticsMethodId = entity.getId();

			TccLogisticsMethodDetailEdit detailList = new TccLogisticsMethodDetailEdit(id, tccLogisticsOne, tccLogisticsTwo, tccLogisticsThree, tccLogisticsFour, tccLogisticsFive,tccLogisticsSix);
			model.addAttribute("detail", detailList);
			model.addAttribute("LogisticsMethodId", LogisticsMethodId);
			model.addAttribute("onProcess", 0);
			return "storage/tccLogisticsMethod/tccLogisticsMethodDetail";
		} catch (Exception e) {
			logger.error("物流方式维护表详情页面出错：", e.getMessage(), e);
			return W.error(model, e.getMessage());
		}

	}
}